package mobidever.mygodutch.bussiness;

import java.util.List;

import android.R.integer;
import android.content.ContentValues;
import android.content.Context;
import mobidever.mygodutch.bussiness.base.BussinessBase;
import mobidever.mygodutch.database.sqlitedal.SQLiteDALAccountBook;
import mobidever.mygodutch.model.ModelAccountBook;

public class BussinessAccountBook extends BussinessBase
{
	private SQLiteDALAccountBook mSqLiteDALAccountBook;

	public BussinessAccountBook(Context pContext)
	{
		super(pContext);
		mSqLiteDALAccountBook = new SQLiteDALAccountBook(pContext);
	}

	public boolean insertModelAccountBook(ModelAccountBook pModelAccountBook)
	{
		mSqLiteDALAccountBook.beginTransaction();

		try
		{
			boolean result1 = mSqLiteDALAccountBook
					.insertAccountBook(pModelAccountBook);
			boolean result2 = true;
			if (pModelAccountBook.getIsDefault() == 1 & result1)
			{
				result2 = setIsDefault((int) pModelAccountBook
						.getAccountBookID());
			}

			if (result1 & result2)
			{
				mSqLiteDALAccountBook.setTransactionSuccessful();
				return true;
			} else
			{
				return false;
			}
		} catch (Exception e)
		{
			return false;
		} finally
		{
			mSqLiteDALAccountBook.endTransaction();
		}

	}

	public boolean deleteModelAccountBookByID(int pID)
	{
		/*
		 * String _Condition = "and AccountBookID = " + pID; return
		 * mSqLiteDALAccountBook.deleteAccountBook(_Condition);
		 */
		return true;
	}

	public boolean updateModelAccountBookByID(ModelAccountBook pModelAccountBook)
	{

		mSqLiteDALAccountBook.beginTransaction();

		try
		{
			boolean result1 = mSqLiteDALAccountBook.updateAccountBook(pModelAccountBook);
			boolean result2 = true;
			if (pModelAccountBook.getIsDefault() == 1 & result1)
			{
				result2 = setIsDefault((int) pModelAccountBook
						.getAccountBookID());
			}

			if (result1 & result2)
			{
				mSqLiteDALAccountBook.setTransactionSuccessful();
				return true;
			} else
			{
				return false;
			}
		} catch (Exception e)
		{
			return false;
		} finally
		{
			mSqLiteDALAccountBook.endTransaction();
		}

	}

	public List<ModelAccountBook> getAccountBooks(String pCondtition)
	{
		return mSqLiteDALAccountBook.getAccountBooks(pCondtition);
	}

	public ModelAccountBook getModelAccountBookByAccountBookID(
			int pAccountBookID)
	{
		List<ModelAccountBook> _List = getAccountBooks("and AccountBookID="
				+ pAccountBookID);
		if (_List.size() == 1)
		{
			return _List.get(0);
		} else
		{
			return null;
		}
	}

	public boolean IsExistAccountBookByAccountBookName(String pAccountBookName,
			Integer pAccountBookID)
	{
		String _Condition = "and AccountBookName='" + pAccountBookName + "'";
		if (pAccountBookID != null)
		{
			_Condition += "and AccountBookID=" + pAccountBookID;
		}
		List<ModelAccountBook> _List = getAccountBooks(_Condition);
		if (_List.size() == 1)
		{
			return true;
		} else
		{
			return false;
		}
	}
	
	public ModelAccountBook getDefaultAccountBook()
	{
		String _Condition = "and IsDefault = 1";
		return getAccountBooks(_Condition).get(0);
	}

	private boolean setIsDefault(int pID)
	{
		String _Condition = "IsDefault = 1";

		ContentValues _ContentValues = new ContentValues();
		_ContentValues.put("IsDefault", 0);
		boolean result1 = mSqLiteDALAccountBook.updateAccountBook(
				_ContentValues, _Condition);

		_Condition = "AccountBookID = " + pID;
		_ContentValues.clear();
		_ContentValues.put("IsDefault", 1);
		boolean result2 = mSqLiteDALAccountBook.updateAccountBook(
				_ContentValues, _Condition);

		if (result1 & result2)
		{
			return true;
		} else
		{
			return false;
		}
	}
}
